package fx.componet;

import fx.protocol.Anchor;
import fx.protocol.ChildrenAddAble;
import fx.protocol.DragAble;
import javafx.scene.Node;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

import java.util.List;

public class MyAnchorPane extends AnchorPane implements Anchor, ChildrenAddAble<Node>, DragAble {
    private double oldStageX;
    private double oldStageY;
    private double oldScreenX;
    private double oldScreenY;

    @Override
    public Node anchorEle() {
        return this;
    }

    @Override
    public ChildrenAddAble<Node> addEle(Node node) {
        childrenEle().add(node);
        return this;
    }

    @Override
    public List<Node> childrenEle() {
        return getChildren();
    }

    @Override
    public void withDrag(Stage primaryStage) {
        this.setOnMousePressed(event -> {
            this.oldStageX = primaryStage.getX();
            this.oldStageY = primaryStage.getY();
            this.oldScreenX = event.getScreenX();
            this.oldScreenY = event.getScreenY();
        });
        this.setOnMouseDragged(event -> {
            primaryStage.setX(event.getScreenX() - this.oldScreenX + this.oldStageX);
            primaryStage.setY(event.getScreenY() - this.oldScreenY + this.oldStageY);
        });
    }
}
